package com.ss.android.common;

import com.bytedance.frameworks.baselib.network.dispatcher.AbsRequest;

import org.json.JSONException;
import org.json.JSONObject;

public abstract class AbsApiThread extends AbsRequest {
    public static final String KEY_DATA = "data";
    public static final String KEY_MESSAGE = "message";
    public static final String STATUS_ERROR = "error";
    public static final String STATUS_SUCCESS = "success";

    public AbsApiThread() {
        this(null, Priority.NORMAL);
    }

    public AbsApiThread(String arg1, Priority arg2) {
        super(arg1, arg2);
    }

    public AbsApiThread(Priority arg2) {
        this(null, arg2);
    }

    public AbsApiThread(String arg2) {
        this(arg2, Priority.NORMAL);
    }

    protected AbsApiThread(String arg2, boolean arg3) {
        this(arg2, arg3 ? Priority.LOW : Priority.NORMAL);
    }

    public AbsApiThread(boolean arg3) {
        this(null, arg3 ? Priority.LOW : Priority.NORMAL);
    }

    public static boolean getHasMore(JSONObject jsonObject, boolean defaultValue) {
        return AbsApiThread.optBoolean(jsonObject, "has_more", defaultValue);
    }

    public static boolean isApiSuccess(String json) {
        if (!android.support.design.a.isTextEmpty(json)) {
            try {
                return AbsApiThread.isApiSuccess(new JSONObject(json));
            } catch (JSONException jsonException) {
                jsonException.printStackTrace();
            }
        }
        return false;
    }

    public static boolean isApiSuccess(JSONObject jo) throws JSONException {
        return jo != null && STATUS_SUCCESS.equals(jo.optString(KEY_MESSAGE));
    }

    public static boolean optBoolean(JSONObject jsonObject, String arg2, boolean arg3) {
        if (jsonObject != null && !jsonObject.isNull(arg2)) {
            int v0 = jsonObject.optInt(arg2, -1);
            if (v0 > 0) {
                arg3 = true;
            } else if (v0 == 0) {
                arg3 = false;
            } else {
                arg3 = jsonObject.optBoolean(arg2, arg3);
            }
        }

        return arg3;
    }
}

